//
//  NWDiscoverDetailViewController.m
//  World
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 小虫. All rights reserved.
//

#import "NWDiscoverDetailViewController.h"
#import "NWDiscoverHeaderView.h"
#import "NWRequestManager.h"
#import "NWDiscoverDetail.h"
#import "NSString+Html.h"
@interface NWDiscoverDetailViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong)NWDiscoverHeaderView *headerView;
@property(nonatomic, strong)NSMutableArray *deadArray;   // 发现详情页面header数据源
@property(nonatomic, strong)NWDiscoverDetail *Modle;
@property(nonatomic, strong)UIWebView *detailView;  // 正文界面
@end

@implementation NWDiscoverDetailViewController

// 懒加载
- (NSMutableArray *)deadArray
{
    if (!_deadArray)
    {
        self.deadArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _deadArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * 2);
    [self.view addSubview:scrollView];
    
    
    _headerView = [[NWDiscoverHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
    [scrollView addSubview:_headerView];
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(10, 10, 40, 40);
    [backButton setImage:[UIImage imageNamed:@"2"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(didBack) forControlEvents:(UIControlEventTouchUpInside)];
    [_headerView addSubview:backButton];
    
    UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    shareButton.frame = CGRectMake(SCREEN_WIDTH - 50,10 , 40, 40);
    [shareButton setImage:[UIImage imageNamed:@"share"] forState:(UIControlStateNormal)];
    [shareButton addTarget:self action:@selector(shareButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [_headerView addSubview:shareButton];
    
    [_headerView.buyButton addTarget:self action:@selector(didBuyButton) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    // 正文界面
    _detailView =[[UIWebView alloc]initWithFrame:CGRectMake(0, 400, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _detailView.backgroundColor = [UIColor redColor];
    [scrollView addSubview:_detailView];
    
    
    
    
    
    [self configureHeaderData];
    
}

// 发现详情header界面的数据请求
- (void)configureHeaderData
{
    [NWRequestManager requestWithType:GET urlString:@"http://api.yizhong.cccwei.com/api.php?srv=2002&cid=413000&uid=0&tms=20151026104620&sig=4f572efb76951cf7&wssig=c7b128bba55a252a&os_type=3&version=8&city_id=2&channel=meizu&article_id=339" parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSDictionary *Pdic = dict[@"data"];
           _Modle = [[NWDiscoverDetail alloc]init];
            [_Modle setValuesForKeysWithDictionary:Pdic];
            _headerView.detailModle = _Modle;
        
        // 解析webView
        NSString *newStr = [NSString importStyleWithHtmlString:_Modle.content];
        [_detailView loadHTMLString:newStr baseURL:[NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath]];

    } error:^(NSError *error) {
        NSLog(@"发现页面数据请求失败%@",error);
    }];
}

// 点击返回发现的主页面
- (void)didBack
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}

// 点击进行分享
- (void)shareButton:(UIButton *)button
{
    NSLog(@"分享");
}

// 点击购买按钮进行购买
- (void)didBuyButton
{
    NSLog(@"购买");
    NWDiscoverDetail *modle = _Modle;
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:modle.buy_url]];
   
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
